package com.jjwxc.jwjskandriod.activity;

import android.graphics.Typeface;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.viewpager.widget.ViewPager;

import com.alibaba.fastjson.JSON;
import com.google.android.material.tabs.TabLayout;
import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.adapter.VipInfoAdapter;
import com.jjwxc.jwjskandriod.config.BaseActivity;
import com.jjwxc.jwjskandriod.config.Constants;
import com.jjwxc.jwjskandriod.config.Url;
import com.jjwxc.jwjskandriod.framework.http.FFNetWorkCallBack;
import com.jjwxc.jwjskandriod.framework.utils.StUtils;
import com.jjwxc.jwjskandriod.model.request.UserProfileRequest;

public class VipInfoActivity extends BaseActivity {

    /**
     * 菜单标题
     */
    private final String[] TAB_TITLES = new String[]{"头像装饰", "主页背景", "阅读背景"};
    ViewPager viewPager;
    TabLayout tabLayout;
    /**
     * 页卡适配器
     */
    private VipInfoAdapter adapter;
    public String mProfileImgUrl;
    public String mBackgroundBox;
    public String mTheme;
    public String mBackground;

    @Override
    public int getContentViewId() {
        return R.layout.activity_vipinfo;
    }

    @Override
    public void findView() {
        viewPager = findViewById(R.id.view_vip_pager);
        tabLayout = findViewById(R.id.tab_vip_layout);


    }

    @Override
    public void setListener() {
        setTitle("装扮背景");
    }

    @Override
    public void afterCreate() {
        mProfileImgUrl = getIntent().getStringExtra(Constants.ProfileImgUrl);
        mBackgroundBox = getIntent().getStringExtra(Constants.backgroundBox);
        mTheme = getIntent().getStringExtra(Constants.Theme);
        mBackground = getIntent().getStringExtra(Constants.Background);
        setTabs(tabLayout, getLayoutInflater(), TAB_TITLES);
        String tab = getIntent().getStringExtra("tab");
        int tabItem;
        initPager();
        if (StUtils.isEmpty(tab)) {
            tabItem=0;
        }else{
            tabItem=Integer.parseInt(tab);
        }
        viewPager.setCurrentItem(tabItem);
        tabLayout.getTabAt(tabItem).select();
    }


    private void initPager() {
        adapter = new VipInfoAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        LinearLayout linearLayout = (LinearLayout) tabLayout.getTabAt(0).getCustomView();
        TextView textView = (TextView) linearLayout.getChildAt(0);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        // 关联切换
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                // 取消平滑切换
                viewPager.setCurrentItem(tab.getPosition(), true);
                int s = tabLayout.getTabCount();
                for (int i = 0; i < s; i++) {
                    if (tab.getPosition() == i) {
                        LinearLayout linearLayout = (LinearLayout) tabLayout.getTabAt(i).getCustomView();
                        TextView textView = (TextView) linearLayout.getChildAt(0);
                        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
                        textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    } else {
                        LinearLayout linearLayout = (LinearLayout) tabLayout.getTabAt(i).getCustomView();
                        TextView textView = (TextView) linearLayout.getChildAt(0);
                        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
                        textView.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    }
                }

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    /**
     * 设置页卡显示效果
     *
     * @param tabLayout
     * @param inflater
     * @param tabTitlees
     */
    private void setTabs(TabLayout tabLayout, LayoutInflater inflater, String[] tabTitlees) {
        for (int i = 0; i < tabTitlees.length; i++) {
            TabLayout.Tab tab = tabLayout.newTab();
            View view = inflater.inflate(R.layout.bookshelf_item_menu, null);
            // 使用自定义视图，目的是为了便于修改，也可使用自带的视图
            tab.setCustomView(view);
            TextView tvTitle = (TextView) view.findViewById(R.id.txt_bookshelf_tab);
            tvTitle.setText(tabTitlees[i]);
            tabLayout.addTab(tab);
        }
    }

    /**
     * 更新用戶信息
     */
    public void updateUserProfile(UserProfileRequest request) {
        post(Url.updateUserProfile, null, String.class, new FFNetWorkCallBack<String>() {
            @Override
            public void onSuccess(String response) {
                if (response.contains("200")) {

                }
            }

            @Override
            public boolean onFail(String response) {
                return false;
            }
        }, JSON.toJSON(request));
    }
}
